package com.pic4493.adapter;

import java.util.ArrayList;

import com.pic4493.adapter.AdpList_PicList.ViewHolder;
import com.pic4493.app.R;
import com.pic4493.app.R.id;
import com.pic4493.entities.ESubscribe;
import com.pic4493.utils.UtiUI;
import com.ppcodes.imageloader.core.DisplayImageOptions;
import com.ppcodes.imageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AbsListView.LayoutParams;

public class AdpGrid_Subscribe extends BaseAdapter
{
   private DisplayImageOptions imgConfig = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc().showStubImage(R.drawable.image_loading).showImageForEmptyUri(R.drawable.image_loading)
		 .bitmapConfig(Bitmap.Config.RGB_565).build();
   private ArrayList<ESubscribe> mList;
   private Context mContext;
   private LayoutInflater mInflater;

   private boolean IsDeleteMode = false;

   public boolean IsDeleteMode()
   {
	  return IsDeleteMode;
   }

   public void setIsDeleteMode()
   {
	  IsDeleteMode = !IsDeleteMode;
	  notifyDataSetChanged();
   }

   public void setIsDeleteMode(boolean isDelete)
   {
	  IsDeleteMode = isDelete;
	  notifyDataSetChanged();
   }

   public AdpGrid_Subscribe(ArrayList<ESubscribe> list, Context context)
   {
	  mList = list;
	  mContext = context;
	  mInflater = LayoutInflater.from(context);
   }

   @Override
   public int getCount()
   {
	  return mList.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int position)
   {
	  return 0;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent)
   {
	  try
	  {
		 ViewHolder holder;
		 if (convertView == null)
		 {
			convertView = mInflater.inflate(R.layout.uimain_gridview_item, null);

			holder = new ViewHolder();
			holder.lay = (LinearLayout) convertView.findViewById(R.id.uimain_item_lay);
			holder.txt = (TextView) convertView.findViewById(R.id.uimain_item_text);
			holder.img = (ImageView) convertView.findViewById(R.id.uimain_item_thumbnail);
			holder.del = (ImageView) convertView.findViewById(R.id.uimain_item_del);
			convertView.setTag(holder);
		 }
		 else
		 {
			holder = (ViewHolder) convertView.getTag();
		 }
		 // LayoutParams params=new
		 // LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		 // convertView.setLayoutParams(params);
		 if ("-3".equals(mList.get(position).getId()))// 增加按键
		 {
			holder.lay.setBackgroundResource(R.drawable.uimain_gditem_add);
			holder.txt.setVisibility(View.GONE);
			holder.img.setVisibility(View.GONE);
		 }
		 else
		 // 其他项
		 {
			holder.lay.setBackgroundResource(0);
			holder.txt.setVisibility(View.VISIBLE);
			holder.img.setVisibility(View.VISIBLE);
			holder.txt.setText(mList.get(position).getName());
			ImageLoader.getInstance().displayImage(mList.get(position).getCover(), holder.img, imgConfig);
		 }

		 if (IsDeleteMode)
		 {
			if (mList.get(position).CanDelete())
			{
			   holder.del.setVisibility(View.VISIBLE);
			   holder.del.setTag(position);
			   holder.del.setOnClickListener(new View.OnClickListener()
			   {
				  @Override
				  public void onClick(View v)
				  {
					 mList.remove(mList.get(Integer.valueOf(v.getTag().toString())));
					 notifyDataSetChanged();
				  }
			   });
			}
			else
			{
			   holder.del.setVisibility(View.INVISIBLE);
			}
		 }
		 else
		 {
			holder.del.setVisibility(View.INVISIBLE);
		 }

		 return convertView;
	  }
	  catch (Exception e)
	  {
		 e.printStackTrace();
	  }
	  return new View(mContext);
   }

   static class ViewHolder
   {
	  LinearLayout lay;
	  TextView txt;
	  ImageView img;
	  ImageView del;
   }
}
